package com.gpframework.module.controller.process;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gpframework.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.process.domain.GpRevisionTable;
import com.gpframework.module.process.domain.vo.GpRevisionTableVo;
import com.gpframework.module.process.domain.req.GpRevisionTableReq;
import com.gpframework.module.process.domain.req.GpRevisionTableAdd;
import com.gpframework.module.process.domain.req.GpRevisionTableUpdate;
import com.gpframework.module.process.service.IGpRevisionTableService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpRevisionTableController
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 改标Controller
 */
@Validated
@Api(tags = "改标")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/process/gevisiontable" )
public class GpRevisionTableController extends BaseController {

    private final IGpRevisionTableService iGpRevisionTableService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-20
  * @Param gpRevisionTable
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('process:table:list')")
    @GetMapping("/list")
    public TableDataInfo<GpRevisionTableVo> list(GpRevisionTableReq gpRevisionTableReq)
    {
        startPage();
        QueryWrapper<GpRevisionTable> lqw = new QueryWrapper<GpRevisionTable>();
        gpRevisionTableReq.generatorQuery(lqw,true);
        List<GpRevisionTableVo> list = iGpRevisionTableService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('process:table:add')" )
    @Log(title = "添加改标" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpRevisionTableAdd gpRevisionTableAdd) {
        GpRevisionTable gpRevisionTable = Convert.convert(new TypeReference<GpRevisionTable>() {}, gpRevisionTableAdd);
        return toAjax(iGpRevisionTableService.save(gpRevisionTable) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('process:table:edit')" )
    @Log(title = "修改改标" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpRevisionTableUpdate> updateList) {
        List<GpRevisionTable> list = Convert.convert(new TypeReference<List<GpRevisionTable>>() {}, updateList);
        return toAjax(iGpRevisionTableService.updateBatchById(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('process:table:export')" )
    @Log(title = "导出改标" , businessType = BusinessType.EXPORT)
    @ApiOperation("导出改标")
    @GetMapping("/export" )
    public AjaxResult export(GpRevisionTableReq gpRevisionTableReq) {
        QueryWrapper<GpRevisionTable> lqw = new QueryWrapper<GpRevisionTable>();
        gpRevisionTableReq.generatorQuery(lqw,true);
        List<GpRevisionTableVo> list = iGpRevisionTableService.listVo(lqw);
        ExcelUtil<GpRevisionTableVo> util = new ExcelUtil<GpRevisionTableVo>(GpRevisionTableVo. class);
        return util.exportExcel(list, "table" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('process:table:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpRevisionTableVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpRevisionTableService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('process:table:remove')" )
    @Log(title = "删除改标" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpRevisionTableService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 审核
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */
    @ApiOperation("审核")
    @PreAuthorize("@ss.hasPermi('process:table:edit')" )
    @Log(title = "审核" , businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody GpRevisionTableUpdate gpRevisionTableUpdate) {
        if (gpRevisionTableUpdate == null || StringUtils.isBlank(gpRevisionTableUpdate.getAuditStatus()) || CollectionUtils.isEmpty(gpRevisionTableUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        UpdateWrapper<GpRevisionTable> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("audit_status", gpRevisionTableUpdate.getAuditStatus());
        if (gpRevisionTableUpdate.getAuditId() != null) {
            updateWrapper.set("audit_id", gpRevisionTableUpdate.getAuditId());
        }
        if (StringUtils.isNotBlank(gpRevisionTableUpdate.getAuditMan())) {
            updateWrapper.set("audit_man", gpRevisionTableUpdate.getAuditMan());
        }
        updateWrapper.set("audit_time", new Date());
        updateWrapper.in("id", gpRevisionTableUpdate.getIds());
        return toAjax(iGpRevisionTableService.update(updateWrapper) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpRevisionTable
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('process:table:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpRevisionTableUpdate gpRevisionTableUpdate) {
        if (gpRevisionTableUpdate == null || gpRevisionTableUpdate.getId() == null || gpRevisionTableUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpRevisionTableUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpRevisionTableService.copy(gpRevisionTableUpdate.getId(), gpRevisionTableUpdate.getCopyNum()) ? 1 : 0);
    }
}
